package com.yonyou.lxp.xutils3test.ui.http;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.yonyou.lxp.xutils3test.MainActivity;
import com.yonyou.lxp.xutils3test.R;
import com.yonyou.lxp.xutils3test.network.Http;
import com.yonyou.lxp.xutils3test.ui.BaseFragment;
import com.yonyou.lxp.xutils3test.ui.ExamActivity;
import com.yonyou.lxp.xutils3test.utils.AppShortCutUtil;
import com.yonyou.lxp.xutils3test.utils.BadgeTools;
import com.yonyou.lxp.xutils3test.utils.BadgeUtil;
import com.yonyou.lxp.xutils3test.utils.Constants;
import com.yonyou.lxp.xutils3test.utils.MakeXml;
import com.yonyou.lxp.xutils3test.utils.PermissionUtils;
import com.yonyou.lxp.xutils3test.utils.SPUtils;
import com.yonyou.lxp.xutils3test.utils.Tools;

import org.xutils.common.util.LogUtil;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.io.File;
import java.io.FileFilter;
import java.text.SimpleDateFormat;
import java.util.Date;

import me.leolin.shortcutbadger.ShortcutBadger;

@ContentView(R.layout.fragment_http)
public class HttpFragment extends BaseFragment {


    @ViewInject(R.id.tv_time)
    private TextView tv_time;
    @ViewInject(R.id.btn_nowtime)
    private Button btn_nowtime;
    @ViewInject(R.id.edt_time)
    private EditText edt_time;
    @ViewInject(R.id.edt_num)
    private EditText edt_num;
    private PermissionUtils permissionUtils;

    private Http http = new Http();

    BadgeTools badgeTools;

    public HttpFragment() {
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        tv_time.setOnClickListener(v -> {
            Toast.makeText(mContext, "1", Toast.LENGTH_LONG).show();
            Toast.makeText(mContext, "2", Toast.LENGTH_LONG).show();
        });
        badgeTools=new BadgeTools(mContext);
        permissionUtils = new PermissionUtils(getActivity());
//        test();
    }

    public static HttpFragment newInstance(String param1, String param2) {
        HttpFragment fragment = new HttpFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * 测试网络请求
     */
    private void test1() {
        String url = Constants.HTTP_URL + Constants.AppUserLogin;
        http.addParams("phoneNumber", "18988768115");
        http.addParams("password", "12345678");
        http.addParams("deviceId", "1233211");
        http.addParams("deviceType", "1");
        http.send(url, new Http.MyCallBack() {
            @Override
            public void fail(String errStr) {
                super.fail(errStr);
            }

            @Override
            public void callback(String result) {
                if (Tools.isSuccess(result)) {
                    SPUtils.put(mContext, Constants.SP_TOKEN, Tools.getJsonStr(Tools.getJsonStr(result, "accessToken"), "accessToken"));

                    LogUtil.d((String) SPUtils.get(mContext, Constants.SP_TOKEN, ""));
                }
            }
        }, mContext, true);
    }


    @Event(value = {R.id.btn_http, R.id.btn_Down, R.id.btn_nowtime, R.id.btn_nowtimeGo, R.id.btn_exam, R.id.btn_ioc})
    private void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_http:
                test1();
                break;
            case R.id.btn_nowtime:
                btn_nowtime.setText(System.currentTimeMillis() + "");
                break;
            case R.id.btn_nowtimeGo:
                toTimeStr(Long.parseLong(btn_nowtime.getText() + ""));
                break;
            case R.id.btn_Down:
                toTimeStr(Long.parseLong(edt_time.getText() + ""));
                MakeXml.main();
                break;
            case R.id.btn_exam:
                Intent intent = new Intent(mContext, ExamActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_ioc:
                doioc();
                break;

        }
    }


    private void doioc() {
//        if (permissionUtils.checkSelfPermission(Manifest.permission.READ_CALENDAR)) {
//            if (AppShortCutUtil.isAddShortCut(mContext)) {
//                AppShortCutUtil.deleteShortCut(mContext, MainActivity.class);
//            }
            AppShortCutUtil.addNumShortCut(mContext, MainActivity.class, true, edt_num.getText().toString(), false);
//        } else {
//            permissionUtils.getPersimmions(Manifest.permission.READ_CALENDAR);
//        }


//        BadgeUtil.setBadgeCount(mContext, 10);
//        ShortcutBadger.applyCount(mContext, 10); //for 1.1.4
//        badgeTools.sendBadgeNumber("10");
//        BadgeUtil.resetBadgeCount(mContext);
    }

    @TargetApi(23)
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        // TODO Auto-generated method stub
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 127) {
            if (AppShortCutUtil.isAddShortCut(mContext)) {
                AppShortCutUtil.deleteShortCut(mContext, MainActivity.class);
            }
            AppShortCutUtil.addNumShortCut(mContext, MainActivity.class, true, "12", true);
        } else {
            LogUtil.e("权限被拒绝了");
        }
    }

    private void toTimeStr(long longTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date(longTime);
        String timeStr = sdf.format(date);
        tv_time.setText(timeStr);
    }

    private void test() {
        FileFilter fileFilter = (File f) -> f.getName().endsWith("*.java");
        final int[] a = {1};
        new Thread(() -> {

            for (int i = 0; i < 100; i++) {
                a[0]++;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                LogUtil.e(a[0] + "");
            }


        }).start();
    }
}
